<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { ChallengeApi } from '@/api/controller'
  import { ToastSuccess, ToastWarning, Toastloading } from '@/utils/public';
  import updateForm from './updateForm.vue';
  import { ElMessageBox } from 'element-plus';
  const updateFormRef = ref()
  const action = reactive({
    list:[] as Array<any>,
    SessionList:[] as Array<any>
  })
  const listQuery = reactive({
    game_id:'',
  })
  const typeChange = ()=>{
    getList()
  }
  const handleForm = (row:any = {})=>{
    updateFormRef.value.openDialog({
      ...row,
      SessionList:action.SessionList
    })
  }
  const handleDelete = (row:any = {})=>{
    const { id=0, item_name='' } = row
    if(!id) return ToastWarning('缺少id')
    ElMessageBox.confirm(`确认删除悬赏id:${id}的奖励配置吗?`, "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(async () => {
      const loading = Toastloading();
      ChallengeApi.offerDelete({id}).then(()=>{
        ToastSuccess("删除成功!");
        loading.close();
        getList();
      }).catch(()=>{
        loading.close();
      })
    }).catch(() => {});
  }
  const getSession = async()=>{
    const loading = Toastloading()
    const res = await ChallengeApi.gameSwitch()
    const { data=[] } = res
    action.SessionList = data
    loading.close()
    getList()
  }
  const getList = async()=>{
    const loading = Toastloading()
    const res = await ChallengeApi.offerList(listQuery)
    const { data=[] } = res || {}
    action.list = data
    loading.close()
  }
  onMounted(() => {
    getSession()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">游戏类型：</div>
          <el-select v-model="listQuery.game_id" @change="typeChange" clearable placeholder="请选择类型">
            <el-option
              v-for="item in action.SessionList"
              :label="item.game_name"
              :value="item.game_id"
            />
          </el-select>
        </div>
        <div class="labelItem">
          <el-button type="primary" @click="typeChange">刷新</el-button>
          <el-button type="primary" @click="handleForm()">新增</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border>
      <el-table-column min-width="120" prop="gameName" label="游戏类型" />
      <el-table-column min-width="100" prop="id" label="悬赏id" />
      <el-table-column min-width="100" prop="rate" label="概率">
        <template #default="scope">{{ scope.row.rate }}%</template>
      </el-table-column>
      <el-table-column min-width="120" prop="offer_draw" label="悬赏人物名称">
        <template #default="scope">
          <span v-for="(item,index) in scope.row.offer_draw" :key="item.item_id">
            <el-tag>{{ item.item_name }}</el-tag>
          </span>
        </template>
      </el-table-column>
      <el-table-column min-width="140" prop="offer_reward" label="奖励礼物id和数量">
        <template #default="scope">
          <span v-for="item of scope.row.offer_reward" :key="item.item_id">
            <el-tag>id:{{ item.gift_id }}，数量{{ item.num }}，金豆数{{ item.jd_num }}</el-tag>
          </span>
        </template>
      </el-table-column>
      <el-table-column min-width="100" prop="second" label="倒计时">
        <template #default="scope">
          {{ scope.row.second }}秒
        </template>
      </el-table-column>
      <el-table-column width="200" fixed=right label="操作">
        <template #default="scope">
          <el-button type="primary" @click="handleForm(scope.row)">编辑</el-button>
          <el-button type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <updateForm ref="updateFormRef" @update-list="getList" />
  </div>
</template>

<style lang='scss' scoped>
  
</style>